package com.woniuxy.education.service.impl;


import com.woniuxy.education.entity.*;
import com.woniuxy.education.mapper.SelectStuDataMapper;
import com.woniuxy.education.service.SelectStuDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
@Service
public class SelectStuDataServiceImpl implements SelectStuDataService {
    @Autowired
    SelectStuDataMapper selectStuDataMapper;
    @Override
    public List<Integer> selectsClass(int g_id) {
        List<Integer> list=new ArrayList();
        list=selectStuDataMapper.selectClass(g_id);

        return list;
    }
    @Override
    public int selectsGrade(int id) {
        int gid=selectStuDataMapper.selectGrade(id);

        return gid;
    }
    //查询xuesheng数据
    @Override
    public SelectStuDataPage selectsStuGrade(int grades1, String testNames,String subject,int currentPage) {
        //创建selectStuDataPage对象，（作为json对象返回前端）
        SelectStuDataPage selectStuDataPage=new SelectStuDataPage();
        //查询总条数
        int a=selectStuDataMapper.selectCountAll(grades1);
        selectStuDataPage.setTotalRecord(a);//总条数
        int b=5;
        selectStuDataPage.setSize(b);//每页大小
        selectStuDataPage.setTotalPage(a/b+1);//总页数

        selectStuDataPage.setCurrentPage(currentPage);//当前页

        List<Student> students=selectStuDataMapper.selectsStuGrade(grades1,(currentPage-1)*b, b);
        List<TestScore> testScores=new ArrayList<>();
        for(Student s:students){
            int u_id=s.getU_id();
            TestScore testScore=selectStuDataMapper.selectByUid(u_id,testNames);
            testScores.add(testScore);
        }

        List<Student> students1=selectStuDataMapper.selectsStuGrade1(grades1);
        List<TestScore> testScores1=new ArrayList<>();
        for(Student s:students1){
            int u_id=s.getU_id();
            TestScore testScore1=selectStuDataMapper.selectByUid(u_id,testNames);
            testScores1.add(testScore1);
        }
        //平均分j---人数，all--总分，p--平均分

        int j=0;
        double all=0;
        if(subject.equals("chinese")){
            for(TestScore t:testScores1){
                all=all + t.getChinese();
                j++;
            }
        }else if(subject.equals("math")){
            for(TestScore t:testScores1){
                all=all+t.getMath();
                j++;
            }
        }else if(subject.equals("english")){
            for(TestScore t:testScores1){
                all=all+t.getEnglish();
                j++;
            }
        }else if(subject.equals("biological")){
            for(TestScore t:testScores1){
                all=all+t.getBiological();
                j++;
            }
        }else if(subject.equals("chemical")){
            for(TestScore t:testScores1){
                all=all+t.getChemical();
                j++;
            }
        }else if(subject.equals("geographic")){
            for(TestScore t:testScores1){
                all=all+t.getGeographic();
                j++;
            }
        }else if(subject.equals("history")){
            for(TestScore t:testScores1){
                all=all+t.getHistory();
                j++;
            }
        }else if(subject.equals("physical")){
            for(TestScore t:testScores1){
                all=all+t.getPhysical();
                j++;
            }
        }else if(subject.equals("political")){
            for(TestScore t:testScores1){
                all=all + t.getPolitical();
                j++;
            }
        }
        BigDecimal alls = new BigDecimal(all);
        BigDecimal js = new BigDecimal(j);
        double p=alls.divide(js,2,BigDecimal.ROUND_HALF_UP).doubleValue();
        String sd="";
        String sdd="";
        int i=0;
        for (Student stu:students) {
            if(subject.equals("chinese")){
                sd=sd+"{number: "+"'"+stu.getNumber()+"'"+
                        ", name: "+"'"+stu.getName()+"'"+
                        ", grade: "+"'"+testScores.get(i).getChinese()+"'"+
                        ", pgrade: "+"'"+p+"'"+
                        " },";
            }else if(subject.equals("math")){
                sd=sd+"{number: "+"'"+stu.getNumber()+"'"+
                        ", name: "+"'"+stu.getName()+"'"+
                        ", grade: "+"'"+testScores.get(i).getMath()+"'"+
                        ", pgrade: "+"'"+p+"'"+
                        " },";
            }else if(subject.equals("political")){
                sd=sd+"{number: "+"'"+stu.getNumber()+"'"+
                        ", name: "+"'"+stu.getName()+"'"+
                        ", grade: "+"'"+testScores.get(i).getPolitical()+"'"+
                        ", pgrade: "+"'"+p+"'"+
                        " },";
            }else if(subject.equals("english")){
                sd=sd+"{number: "+"'"+stu.getNumber()+"'"+
                        ", name: "+"'"+stu.getName()+"'"+
                        ", grade: "+"'"+testScores.get(i).getEnglish()+"'"+
                        ", pgrade: "+"'"+p+"'"+
                        " },";
            }else if(subject.equals("physical")){
                sd=sd+"{number: "+"'"+stu.getNumber()+"'"+
                        ", name: "+"'"+stu.getName()+"'"+
                        ", grade: "+"'"+testScores.get(i).getPhysical()+"'"+
                        ", pgrade: "+"'"+p+"'"+
                        " },";
            }else if(subject.equals("history")){
                sd=sd+"{number: "+"'"+stu.getNumber()+"'"+
                        ", name: "+"'"+stu.getName()+"'"+
                        ", grade: "+"'"+testScores.get(i).getHistory()+"'"+
                        ", pgrade: "+"'"+p+"'"+
                        " },";
            }else if(subject.equals("geographic")){
                sd=sd+"{number: "+"'"+stu.getNumber()+"'"+
                        ", name: "+"'"+stu.getName()+"'"+
                        ", grade: "+"'"+testScores.get(i).getGeographic()+"'"+
                        ", pgrade: "+"'"+p+"'"+
                        " },";
            }else if(subject.equals("chemical")){
                sd=sd+"{number: "+"'"+stu.getNumber()+"'"+
                        ", name: "+"'"+stu.getName()+"'"+
                        ", grade: "+"'"+testScores.get(i).getChemical()+"'"+
                        ", pgrade: "+"'"+p+"'"+
                        " },";
            }else if(subject.equals("biological")){
                sd=sd+"{number: "+"'"+stu.getNumber()+"'"+
                        ", name: "+"'"+stu.getName()+"'"+
                        ", grade: "+"'"+testScores.get(i).getBiological()+"'"+
                        ", pgrade: "+"'"+p+"'"+
                        " },";
            }

            i++;
        }
        sdd="["+sd+"]";
        selectStuDataPage.setStudata(sdd);
        return selectStuDataPage;
    }
    @Override
    public int selectsGrade1(int grades1){
        int i=selectStuDataMapper.selectGrade(grades1);
        return i;
    };
    @Override
    public TongJiTuData selectsClass1(int grades1,String testNames,String subject){
        TongJiTuData td=new TongJiTuData();

        List<GradeClass> list=selectStuDataMapper.selectsClass1(grades1);
        String classNames1="";
        for(int i=0;i<list.size();i++){

           String className= selectStuDataMapper.selectsClassNames(list.get(i).getId()).getCname();
            System.out.println(className);
            classNames1=classNames1+"'"+className+"',";
        }
        classNames1="["+classNames1+"'"+"年级平均分"+"'"+"]";
        td.setClasslist(classNames1);

        String score="";
        double pall=0;//年级总分
        double pallss;//年级平均分
        int num=0;
        for(int i=0;i<list.size();i++){
            List<Student> students2= selectStuDataMapper.selectsStuGrade1(list.get(i).getId());
            List<TestScore> testScores2=new ArrayList<>();
            for(Student s:students2) {
                int u_id = s.getU_id();
                TestScore testScore2 = selectStuDataMapper.selectByUid(u_id, testNames);
                testScores2.add(testScore2);
            }
                //平均分j---人数，all--总分，p--平均分
                int j=0;
                double all=0;
                if(subject.equals("chinese")){
                    for(TestScore t:testScores2){
                        all=all + t.getChinese();
                        j++;
                    }
                }else if(subject.equals("math")){
                    for(TestScore t:testScores2){
                        all=all+t.getMath();
                        j++;
                    }
                }else if(subject.equals("english")){
                    for(TestScore t:testScores2){
                        all=all+t.getEnglish();
                        j++;
                    }
                }else if(subject.equals("biological")){
                    for(TestScore t:testScores2){
                        all=all+t.getBiological();
                        j++;
                    }
                }else if(subject.equals("chemical")){
                    for(TestScore t:testScores2){
                        all=all+t.getChemical();
                        j++;
                    }
                }else if(subject.equals("geographic")){
                    for(TestScore t:testScores2){
                        all=all+t.getGeographic();
                        j++;
                    }
                }else if(subject.equals("history")){
                    for(TestScore t:testScores2){
                        all=all+t.getHistory();
                        j++;
                    }
                }else if(subject.equals("physical")){
                    for(TestScore t:testScores2){
                        all=all+t.getPhysical();
                        j++;
                    }
                }else if(subject.equals("political")){
                    for(TestScore t:testScores2){
                        all=all + t.getPolitical();
                        j++;
                    }
                }
                BigDecimal alls = new BigDecimal(all);
                BigDecimal js = new BigDecimal(j);
                double p=alls.divide(js,2,BigDecimal.ROUND_HALF_UP).doubleValue();
                pall=pall+p;
                score=score+p+",";
                num++;
            }
        BigDecimal palls = new BigDecimal(pall);
        BigDecimal nums = new BigDecimal(num);
        pallss=palls.divide(nums,2,BigDecimal.ROUND_HALF_UP).doubleValue();
        score="["+score+pallss+"]";
        td.setPscore(score);
        return td;
    };

    public TongJiTuData selectsClassdata(int grades,int grades1, String subject){
        TongJiTuData td=new TongJiTuData();
        List<GradeClass> list=selectStuDataMapper.selectsClass1(grades1);
        List<String> testNames=new ArrayList<>();
        testNames.add("高一上第一次月考");
        testNames.add("高一上第二次月考");
        testNames.add("高一上第三次月考");
        List cscore=new ArrayList();//班级平均分数
        List gscore=new ArrayList();//年级平均分数
        for(int a=0;a<testNames.size();a++){

            double classp=0;//班级平均分
            double pall=0;//年级总分
            double pallss=0;//年级平均分
            int num=0;
            for(int i=0;i<list.size();i++){

                List<Student> students2= selectStuDataMapper.selectsStuGrade1(list.get(i).getId());
                List<TestScore> testScores2=new ArrayList<>();
                for(Student s:students2) {
                    int u_id = s.getU_id();
                    TestScore testScore2 = selectStuDataMapper.selectByUid(u_id, testNames.get(a));
                    testScores2.add(testScore2);
                }
                //平均分j---人数，all--总分，p--平均分
                int j=0;
                double all=0;
                if(subject.equals("chinese")){
                    for(TestScore t:testScores2){
                        all=all + t.getChinese();
                        j++;
                    }
                }else if(subject.equals("math")){
                    for(TestScore t:testScores2){
                        all=all+t.getMath();
                        j++;
                    }
                }else if(subject.equals("english")){
                    for(TestScore t:testScores2){
                        all=all+t.getEnglish();
                        j++;
                    }
                }else if(subject.equals("biological")){
                    for(TestScore t:testScores2){
                        all=all+t.getBiological();
                        j++;
                    }
                }else if(subject.equals("chemical")){
                    for(TestScore t:testScores2){
                        all=all+t.getChemical();
                        j++;
                    }
                }else if(subject.equals("geographic")){
                    for(TestScore t:testScores2){
                        all=all+t.getGeographic();
                        j++;
                    }
                }else if(subject.equals("history")){
                    for(TestScore t:testScores2){
                        all=all+t.getHistory();
                        j++;
                    }
                }else if(subject.equals("physical")){
                    for(TestScore t:testScores2){
                        all=all+t.getPhysical();
                        j++;
                    }
                }else if(subject.equals("political")){
                    for(TestScore t:testScores2){
                        all=all + t.getPolitical();
                        j++;
                    }
                }
                BigDecimal alls = new BigDecimal(all);
                BigDecimal js = new BigDecimal(j);
                double p=alls.divide(js,2,BigDecimal.ROUND_HALF_UP).doubleValue();
                pall=pall+p;
                num++;
                if(list.get(i).getId()==grades){
                    classp=p;
                }
            }
            BigDecimal palls = new BigDecimal(pall);
            BigDecimal nums = new BigDecimal(num);
            pallss=palls.divide(nums,2,BigDecimal.ROUND_HALF_UP).doubleValue();

            cscore.add(classp);
            gscore.add(pallss);
        }
        String s="";
        for(int b=0;b<testNames.size();b++){
            s=s+"[" +"'"+testNames.get(b)+"',"+cscore.get(b)+","+gscore.get(b)+"],";
        }
        String ss="["+"["+"'product',"+"'本班级平均成绩',"+"'年级平均成绩'"+"],"+s+"]";

        td.setClassdata(ss);
        return td;
    };
    public TongJiTuData selectsGradeRank(int grades1, String testNames, String subject){

        TongJiTuData td=new TongJiTuData();
        List<Student> students2= selectStuDataMapper.selectsStuGrade1(grades1);
        List<TestScore> testScores2=new ArrayList<>();
        for(Student s:students2) {
            int u_id = s.getU_id();
            TestScore testScore2 = selectStuDataMapper.selectByUid(u_id, testNames);
            testScores2.add(testScore2);
        }
        int a=0,b=0,c=0,d=0,e=0;
        if(subject.equals("chinese")) {
            for (TestScore t : testScores2) {
                if (t.getChinese() < 60) {
                    a++;
                } else if (t.getChinese() < 70) {
                    b++;
                } else if (t.getChinese() < 80) {
                    c++;
                } else if (t.getChinese() < 90) {
                    d++;
                } else if (t.getChinese() <= 100) {
                    e++;
                }
            }
        }else if(subject.equals("math")){
            for(TestScore t:testScores2){
                if(t.getMath()<60){
                    a++;
                }else if(t.getMath()<70){
                    b++;
                }else if(t.getMath()<80){
                    c++;
                }else if(t.getMath()<90){
                    d++;
                }else if(t.getMath()<=100){
                    e++;
                }
            }
        }else if(subject.equals("english")){
            for(TestScore t:testScores2){
                if(t.getEnglish()<60){
                    a++;
                }else if(t.getEnglish()<70){
                    b++;
                }else if(t.getEnglish()<80){
                    c++;
                }else if(t.getEnglish()<90){
                    d++;
                }else if(t.getEnglish()<=100){
                    e++;
                }
            }
        }else if(subject.equals("biological")){
            for(TestScore t:testScores2){
                if(t.getBiological()<60){
                    a++;
                }else if(t.getBiological()<70){
                    b++;
                }else if(t.getBiological()<80){
                    c++;
                }else if(t.getBiological()<90){
                    d++;
                }else if(t.getBiological()<=100){
                    e++;
                }
            }
        }else if(subject.equals("chemical")){
            for(TestScore t:testScores2){
                if(t.getChemical()<60){
                    a++;
                }else if(t.getChemical()<70){
                    b++;
                }else if(t.getChemical()<80){
                    c++;
                }else if(t.getChemical()<90){
                    d++;
                }else if(t.getChemical()<=100){
                    e++;
                }
            }
        }else if(subject.equals("geographic")){
            for(TestScore t:testScores2){
                if(t.getGeographic()<60){
                    a++;
                }else if(t.getGeographic()<70){
                    b++;
                }else if(t.getGeographic()<80){
                    c++;
                }else if(t.getGeographic()<90){
                    d++;
                }else if(t.getGeographic()<=100){
                    e++;
                }
            }
        }else if(subject.equals("history")){
            for(TestScore t:testScores2){
                if(t.getHistory()<60){
                    a++;
                }else if(t.getHistory()<70){
                    b++;
                }else if(t.getHistory()<80){
                    c++;
                }else if(t.getHistory()<90){
                    d++;
                }else if(t.getHistory()<=100){
                    e++;
                }
            }
        }else if(subject.equals("physical")){
            for(TestScore t:testScores2){
                if(t.getPhysical()<60){
                    a++;
                }else if(t.getPhysical()<70){
                    b++;
                }else if(t.getPhysical()<80){
                    c++;
                }else if(t.getPhysical()<90){
                    d++;
                }else if(t.getPhysical()<=100){
                    e++;
                }
            }
        }else if(subject.equals("political")){
            for(TestScore t:testScores2){
                if(t.getPolitical()<60){
                    a++;
                }else if(t.getPolitical()<70){
                    b++;
                }else if(t.getPolitical()<80){
                    c++;
                }else if(t.getPolitical()<90){
                    d++;
                }else if(t.getPolitical()<=100){
                    e++;
                }
            }
        }
        String s="["+a+","+b+","+c+","+d+","+e+"]";

        td.setRank(s);
        return td;
    };
}



